﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Business;
using System.Collections.ObjectModel;
using System.Collections;
using Microsoft.Practices.Composite.Events;
using PJ.Business.SalePurchase;
using System.Windows;

namespace PJ.Presentation.SalePurchase
{
    public abstract class ShoppingCartModel : EntityViewModel, IShoppingCartModel
    {

        protected ShoppingCartModel()
        {
        }

        protected ShoppingCartModel(string globalId, IShoppingCartService service)
            : base(globalId, service)
        {
        }

        protected ShoppingCartModel(IView view, IEventAggregator aggregator, IShoppingCartService service)
            : base(view, aggregator, service)
        {
        }

        public void SetPartner(EntityEventArg arg)
        {
            if (arg == null)
                SetPartner(null as string);
            else
                SetPartner(arg.Id);
        }

        public void SetPartner(string id)
        {
            if (IsStringNull(id))
                SetValues(null as IBusinessObject, true);
            else
            {
                IShoppingCart obj = (IShoppingCart)Service.GetById(id, PJ.Data.GetDataMode.CachedFind);
                if (obj == null)
                {
                    obj = (IShoppingCart)Service.CreateNewItem();
                    obj.Id = id;
                    obj = (IShoppingCart)Service.PersistAndGet(obj);
                }
                SetValues(obj, true);
            }
        }

        /// <summary>
        /// GetValues. Sets the businness object properties using the model property values
        /// </summary>
        /// <param name="businessObject">business object to update</param>
        public override IBusinessObject GetValues()
        {
            if (base.GetValues() == null)
            {
                if (IsStringNull(Reference))
                    return null;
            }
            BusinessObject.Reference = Reference;
            return BusinessObject;
        }
        /// <summary>
        /// SetValues. Sets the model properties using the businessobject property values
        /// </summary>
        /// <param name="businessObject"></param>
        public override void SetValues(IBusinessObject businessObject, bool saveBefore)
        {
            base.SetValues(businessObject, saveBefore);
            if (businessObject == null)
            {
                Reference = null;
            }
            else
            {
                Reference = BusinessObject.Reference;
            }
        }


        public string Reference
        {
            get { return (string)ThreadUnsafeGetValue(ReferenceProperty); }
            set { ThreadUnsafeSetValue(ReferenceProperty, value); }
        }

        public static readonly DependencyProperty ReferenceProperty =
            DependencyProperty.Register("Reference", typeof(string), typeof(ShoppingCartModel), new UIPropertyMetadata(""));

        #region Protected

        protected override bool ValuesChanged()
        {
            if (base.ValuesChanged())
                return true;
            else
            {
                if (IsBusinessObjectNull)
                {
                    if (IsStringNull(Reference))
                        return false;
                }
                if (StringEqual(Reference, BusinessObject.Reference))
                    return false;
                else
                    return true;
            }
        }

        new protected IShoppingCartService Service
        {
            get
            {
                return (IShoppingCartService)base.Service;
            }
        }

        protected new IShoppingCart BusinessObject
        {
            get { return base.BusinessObject as IShoppingCart; }
        }

        #endregion

        #region Field

        private string _Initial_Reference;

        #endregion

    }
}
